/**
 * 用戶操作類
 */
function userEvent() {
	/**
	 * 本類函數調用事件
	 */
	if ( userEvent.prototype.handleFun == undefined ) {
		userEvent.prototype.handleFun = function(r_room, r_user, r_param) {
			var _fun_name = r_param.type
			this[_fun_name](r_room,r_user,r_param);
		}
	}
	/**
	 * 设置用户的variable属性
	 */
	if ( userEvent.prototype.setUserVariable == undefined ) {
		userEvent.prototype.setUserVariable = function(r_user, r_param, r_broad) {					
			_server.setUserVariables(r_user, r_param, r_broad);			
		}
	}
	/**
	 * 設置玩家坐下
	 * 更改用戶狀態，更改用戶遊戲id為遊戲座位
	 * @param	r_room	玩家所在房間
	 * @param	r_user	玩家對象
	 * @param	r_index	玩家座位號
	 */
	if ( userEvent.prototype.userSitDown == undefined ) {
		userEvent.prototype.userSitDown = function(r_room,r_user,r_index) {
			this.setUserState(r_room, r_user, 'play');
			r_user.setPlayerIndex(r_room, r_index);
		}
	}
	
	/**
	 * 會員叫牌事件
	 * @param	r_room	Room	房間
	 * @param	r_user	User	用戶
	 * @param	r_param	object	參數對象集
	 * @return	object	執行結果
	 */
	if ( userEvent.prototype.actionPoker == undefined ) {
		userEvent.prototype.actionPoker = function(r_room, r_user, r_param) {
			var _poker_action = new pokerAction();
			return _poker_action.handleFun(fromRoom, user, params.type, params.pot);
		}
	}
	/**
	 * 用戶驗證
	 * 檢查用戶是否丟失
	 */
	if ( userEvent.prototype.checkLose == undefined ) {
		userEvent.prototype.checkLose = function(r_user) {
			if (r_user.isEmpty()) {
				trace("lose");
			}
		}
	}
	/**
	 * 用户叫牌計時
	 * @param	r_room	操作房間
	 * @param	r_index	房間座位索引
	 */
	if ( userEvent.prototype.userEventTime == undefined ) {
		userEvent.prototype.userEventTime = function(r_room, r_index) {		
			trace("userEvent userEventTime");
			var _index_arr = r_room.properties.get("indexArr");			
			var _index = parseInt(r_room.getVariable("index").value);
			var _just = r_room.getVariable("just");
			_just = (_just == undefined) ? false : (_just.value);
			if ((_index == r_index || r_index == -1 || _index_arr.length <= 1) && _just == false ) {
				__gameEvent.checkGameOver(r_room);
				return;
			}			
			var _player_count = _index_arr.length;
			r_index = ( r_index >= _player_count ) ? ( r_index - _player_count ) : r_index;
			r_index = (_just == true) ? 0 : r_index;
			setRoomVariable(r_room, { index:r_index , just:false }, false);			
			var _user = r_room.getUserByPlayerIndex(_index_arr[r_index]);	
			__log.show(_index_arr[r_index]+' is reading to deal');
			var _task = new _scheduler.Task( { room:r_room.getId() } );
			__task.remove(r_room.getId());
			__task.thread[r_room.getId()] = _task;
			var handlerObj = { };
			handlerObj['params'] = { user:_user.getUserId() };
			if (parseInt(_user.getVariable("level").value) == 100) {
				__AIEvent.deal(r_room, _user);
				return;
			}
			handlerObj.doTask = function(task) {
				// 玩家叫牌超時，廣播到當前房間所以用戶，同時遊戲進入下一步	
				var _room = _zone.getRoom(task.id.room);
				var _user = _server.getUserById(this.params.user);
				if (_user == null) {
					return;//玩家已离开
				}
				trace(_user.getVariable("name").value + " is time out!");
				//超時默認棄牌
				task.active = false;				
				_pokerAc.bet(_room, _user, { way:2, chip:0 } );				
			}
			var taskHandler = new _scheduler.ITaskHandler(handlerObj);
			scheduler.addScheduledTask(_task, 60, false, taskHandler);
			var _param = { };
			_param._cmd = "next";
			_param.dataObj = { uid:parseInt(r_room.getUserByPlayerIndex(_index_arr[r_index]).getName()) };
			_server.sendResponse(_param, -1, null, r_room.getAllUsers(), 'json');
		}
	}
	/**
	 * 取消用戶叫牌計時，並開始下一用戶叫牌
	 * @param	r_room	房間對象
	 * @param	r_user	用戶對象
	 */
	if ( userEvent.prototype.nextUserAction == undefined ) {
		userEvent.prototype.nextUserAction = function(r_room, r_user) {	
			trace("userEvent nextUserAction");
			if ( !r_room.isGame() ) {
				return false;//房間不是遊戲房間
			}
			if ( r_user.isSpectator() ) {
				return false;//用戶正在旁觀狀態
			}			
			if (parseInt(r_room.getVariable("state").value) != 2 ) {
				return;
			}			
			var _news = parseInt(r_room.getVariable("news").value);
			var _index_arr = r_room.properties.get("indexArr");
			_index_arr = (_index_arr == null) ? Array() : _index_arr;
			var _index_obj = r_room.getVariable("index");
			var _index = (_index_obj == null) ? -1 : parseInt(_index_obj.value);
			var _temp_index = _index;
			_index = this.getNextUserBet(r_room, _index);
			//取消現有玩家計時
			__task.remove(r_room.getId());
			//開始發牌,遊戲結束判斷
			var _deals = r_room.getVariable("deals");
			_deals = (_deals == null ) ? 0 : parseInt(_deals.value);			
			var _just = r_room.getVariable("just");
			_just = (_just == undefined) ? false : (_just.value);
			
			if ( (_news == _index_arr[_index] && _deals != 0 && _just == false) || _index == -1) {			
				__gameEvent.sendPotData(r_room);
				__gameEvent.reDeals(r_room);
				
				return;		
			}
			if (_deals == 0) {
				setRoomVariable(r_room, { deals:1 }, false);				
			}
			this.userEventTime(r_room, _index);			
		}
	}
}


/**
 * 设置玩家状态：玩家狀態，玩家旁觀，玩家离开(进入大厅)
 * @param r_user	玩家对象
 * @param r_type	状态类型   sit | play | spectator
 * @param r_broad	是否广播
 */
userEvent.prototype.setUserState = function(r_room, r_user, r_type, r_sit) {			
	var _room_id = r_room.getId();
	var _data = { };				
	switch(r_type) {
		case "sit":	
			if(r_sit > 0){
				r_user.setPlayerIndex(r_room, r_sit);
			}
			__userVars.setState(r_user, "sit");	
			break;
		case "play":
			//_server.switchSpectator(r_user, _room_id, false);				
			var _sit = r_user.getPlayerIndex();
			if(r_sit > 0){
				r_user.setPlayerIndex(r_room, r_sit);				
			}
			__userVars.setState(r_user, "play");				
			break;
		case "spectator":			
			_server.switchPlayer(r_user, _room_id, false);		
			__userVars.setState(r_user,r_type);	
			break;						
	}
}


/**
 * 兑换筹码
 * @param	r_user	玩家对象
 * @param	r_chip	兑换筹码数
 */
userEvent.prototype.exchangeChip = function(r_user, r_chip) {
	var _gold = __userVars.getVar(r_user, "gold", "int");
	var _uid = __userVars.getVar(r_user, "userId", "int");
	if (_gold > r_chip) {
		_gold -= r_chip;
	}else {
		r_chip = _gold;
		_gold = 0;
	}
	__userVars.setVar(r_user, { gold : _gold, chip : r_chip } );
	var _sql = "UPDATE texas_member SET gold = gold - %d, chip = chip + %d  WHERE member_id='%d' ";
	var _arg = [r_chip , r_chip, _uid];
	__conn.execute(_sql, _arg);
	return r_chip;
}

/**
 * 将筹码退会到点数
 */
userEvent.prototype.exchangeGold = function(r_user) {
	var _uid = __userVars.getVar(r_user, "userId", "int");	
	var _chip = __userVars.getVar(r_user, "chip", "int");
	var _gold = __userVars.getVar(r_user, "gold", "int");
	_gold += _chip;
	__userVars.setVar(r_user, { gold : _gold , chip : 0 } );
	var _sql = "UPDATE texas_member SET gold = gold + %d, chip = 0 WHERE member_id='%d' ";
	var _arg = [_chip , _uid];
	__conn.execute(_sql, _arg);		
	return _gold;
}

/**
 * 扣除玩家点数
 */
userEvent.prototype.subtractGold = function(r_user, r_sub) {
	var _gold = __userVars.getVar(r_user, "gold", "int");
	if (_gold < r_sub) {
		r_sub = _gold;
	}
	_gold -= r_sub;
	__userVars.setVar(r_user, "gold", "int");
	var _uid = __userVars.getVar(r_user, "userId", "int");
	var _sql = "UPDATE texas_member SET gold = gold - %d, chip = 0 WHERE member_id='%d' ";
	var _arg = [r_sub , _uid];
	__conn.execute(_sql, _arg);		
	return _gold;
}

/**
  * 获取多个玩家数据
  * @param r_users 
  * @return [[],[],...]
  */
userEvent.prototype.getUserListVars = function(r_user_arr) {
	var _list = [];
	for each(var _user in r_user_arr) {
		_list.push(__userVars.getUserVars(_user) );
	}
	return _list;
}


/**
 * 取得下一个叫牌玩家的座位号索引
 * @param	r_room	所在房间
 * @param	r_index	相对座位
 */
userEvent.prototype.getNextUserBet = function(r_room,r_index) {
	var _index_arr = r_room.properties.get("indexArr");
	_index_arr = (_index_arr == null) ? Array() : _index_arr;
	if (_index_arr) {
		var _play_count = _index_arr.length;
		if (_play_count == 0) {
			//__gameEvent.checkGameOver(r_room);
			return -1;
		}
		if (_play_count == 1) {
			return 0;
		}
		var _index = r_index + 1;
		_index = ( _index >= _play_count ) ? ( _index - _play_count ) : _index;
		return _index;
	}
	return 0;
}


/**
 * qu
 */
userEvent.prototype.setPreUserBet = function(r_room) {	
	var _index = parseInt(r_room.getVariable("index").value);
	var _index_arr = r_room.properties.get("indexArr");
	_index_arr = (_index_arr == null) ? Array() : _index_arr;
	var _temp_index = _index;
	_index = (_index != 0) ? (_index - 1) : (_index_arr.length -1);
	_index = (_index < 0) ? 0 : _index;
	trace("current index is " + _index +"  pre index is " + _temp_index);
	setRoomVariable(r_room, { index:_index }, false);
}
